<template>
  <div class="app-container">
    <sticky class="filter-container">
      <el-row :gutter="20">
        <el-col :span="21">
          <div class="block" style="float: left; margin-right: 3px">
            <el-date-picker
              v-model="datePicker"
              type="datetimerange"
              :picker-options="pickerOptions"
              format="yyyy-MM-dd HH:mm"
              value-format="yyyy-MM-dd HH:mm"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              size="mini"
              @change="handleSearch"
            />
          </div>
          <el-input
            v-model="listSearch.search"
            placeholder="搜索"
            style="margin-right: 3px;width: 200px"
            class="filter-item"
            size="mini"
            clearable
            @keyup.enter.native="handleSearch"
            @clear="handleSearch"
            @blur="handleSearch"
          />
          <el-select
            v-model="listSearch.status"
            placeholder="状态"
            clearable
            class="filter-item"
            size="mini"
            style="margin-right: 10px;width: 80px"
            @change="handleSearch"
          >
            <el-option
              v-for="item in enabledTypeOptions"
              :key="item.key"
              :label="item.display_name"
              :value="item.key"
            />
          </el-select>
          <el-button
            v-waves
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-search"
            @click="handleSearch"
          >
            搜索
          </el-button>
          <el-button
            v-waves
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-refresh-right"
            :loading="refreshLoading"
            @click="refresh"
          />
        </el-col>
        <el-col :span="3" style="text-align: right">
          <el-dropdown v-if="checkPermission(['admin', 'landing_all', 'landing_export'])" trigger="click">
            <el-button
              v-waves
              :loading="downLoading"
              class="filter-item"
              size="mini"
              type="primary"
              icon="el-icon-download"
            >
              导出
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleDownloadCurrent">导出当前</el-dropdown-item>
              <el-dropdown-item @click.native="handleDownloadAll">导出所有</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </sticky>
    <el-table
      border
      :data="list"
      style="width: 100%"
      highlight-current-row
      :default-sort="{ prop: '-time', order: 'descending' }"
      @sort-change="sortChange"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="登陆用户">
              <span>{{ props.row.username }}</span>
            </el-form-item>
            <el-form-item label="用户名">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="时区">
              <span>{{ props.row.tz }}</span>
            </el-form-item>
            <el-form-item label="类型">
              <span>{{ props.row.type }}</span>
            </el-form-item>
            <el-form-item label="代理IP">
              <span>{{ props.row.agent }}</span>
            </el-form-item>
            <el-form-item label="IP地址">
              <span>{{ props.row.ip }}</span>
            </el-form-item>
            <el-form-item label="IP归属">
              <span>{{ props.row.city }}</span>
            </el-form-item>
            <el-form-item label="说明">
              <span>{{ props.row.why }}</span>
            </el-form-item>
            <el-form-item label="状态">
              <el-tag :type="props.row.status ? 'success':'danger'">{{ props.row.status ? '成功':'失败' }}</el-tag>
            </el-form-item>
            <el-form-item label="日期">
              <span>{{ props.row.time }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
        label="登陆用户"
        prop="username"
        align="center"
      />
      <el-table-column
        label="用户名"
        prop="name"
        align="center"
      />
      <el-table-column
        label="时区"
        prop="tz"
        align="center"
      />
      <el-table-column
        label="代理IP"
        prop="agent"
        width="130px"
        align="center"
      />
      <el-table-column
        label="IP地址"
        prop="ip"
        width="130px"
        align="center"
      />
      <el-table-column
        label="IP归属"
        prop="city"
        align="center"
      />
      <el-table-column
        label="说明"
        prop="why"
        align="center"
      />
      <el-table-column
        label="状态"
        prop="status"
        width="70px"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.status ? 'success':'danger'">{{ scope.row.status ? '成功':'失败' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="日期"
        prop="time"
        align="center"
        width="155px"
        sortable="custom"
      />
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      style="text-align: right; margin-top: 20px"
      background
      :hide-on-single-page="true"
      :current-page="listSearch.page"
      :page-size="listSearch.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listSearch.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
// 按钮吸顶
import Sticky from '../../../components/Sticky/index'
// 按钮水波效果
import waves from '../../../directive/waves/waves'
import checkPermission from '@/utils/permission'
import { listLanDing, exportUserAll, exportUserCurrent } from '@/api/landing'

export default {
  components: { Sticky },
  directives: { waves },
  data() {
    return {
      datePicker: null,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      url_api: process.env.VUE_APP_BASE_API,
      refreshLoading: false,
      downLoading: false,
      listLoading: true,
      list: [],
      listSearch: {
        total: 0,
        page: 1,
        pageSize: 10,
        ordering: '-time',
        is_active: '',
        search: '',
        start_date: '',
        end_date: ''
      },
      enabledTypeOptions: [
        { key: 'true', display_name: '成功' },
        { key: 'false', display_name: '失败' }
      ]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // checkPermission 权限检查
    checkPermission,
    // 弹出框
    // 搜索
    handleSearch() {
      // 当页面不在第一页时搜索内容报错
      this.listSearch.page = 1
      this.dateFileter()
      this.getList()
    },
    dateFileter() {
      // 日期时间范围选择
      if (this.datePicker !== null) {
        this.listSearch.start_date = this.datePicker[0]
        this.listSearch.end_date = this.datePicker[1]
      } else {
        this.listSearch.start_date = ''
        this.listSearch.end_date = ''
      }
    },
    // 搜索
    // 排序
    sortChange(column) {
      const { prop, order } = column
      console.log(order)
      if (order === 'descending') {
        this.listSearch.ordering = `-${prop}`
      } else {
        this.listSearch.ordering = prop
      }
      this.handleSearch()
    },
    // 排序
    // 导出
    handleDownloadCurrent() {
      this.downLoading = true
      exportUserCurrent(this.listSearch).then(res => {
        const url = this.url_api + 'media/export/' + res.data.name
        window.open(url)
        this.downLoading = false
        this.$notify.success({
          type: 'success',
          title: '成功',
          message: res.data.name + res.message,
          showClose: true,
          duration: 5 * 1000
        })
      })
    },
    handleDownloadAll() {
      this.downLoading = true
      exportUserAll().then(res => {
        const url = this.url_api + 'media/export/' + res.data.name
        window.open(url)
        this.downLoading = false
        this.$notify.success({
          type: 'success',
          title: '成功',
          message: res.data.name + res.message,
          showClose: true,
          duration: 5 * 1000
        })
      })
    },
    // 导出
    // 列表
    getList() {
      this.listLoading = true
      listLanDing(this.listSearch).then(res => {
        this.list = res.data
        this.listSearch.total = res.count
        this.listLoading = false
      })
    },
    // 列表
    // 分页
    handleSizeChange(val) {
      this.listSearch.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listSearch.page = val
      this.getList()
    },
    // 分页
    // 刷新
    refresh() {
      this.refreshLoading = true
      this.getList()
      this.refreshLoading = false
      this.$notify.info({
        type: 'info',
        title: '提示',
        message: '已刷新',
        showClose: true,
        duration: 5 * 1000
      })
    }
  }
}
</script>

<style scoped>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
